import {
  SAVE_ALLCHANNELS,
  SAVE_ARTICLE_LIST,
  SAVE_CHANNELS,
} from '../action_type/home'

const initValue = {
  userChannels: [],
  allChannels: [],
  articles: {}, //存储所有的文章了列表
}
export default function reducer(state = initValue, action) {
  const { type, payload } = action
  switch (type) {
    case SAVE_CHANNELS:
      return {
        ...state,
        userChannels: payload,
      }
    case SAVE_ALLCHANNELS:
      return {
        ...state,
        allChannels: payload,
      }

    case SAVE_ARTICLE_LIST:
      const { list, timestamp, loadMore, channelId } = payload
      // const oldList = state.articles[channelId].list
      // 如果loadMore为true 代码加载更多数据，不应该覆盖，应该追加
      return {
        ...state,
        articles: {
          ...state.articles,
          [channelId]: {
            timestamp: timestamp,
            // 如果是loadMore 追加数据 否则覆盖数据
            list: loadMore ? [...state.articles[channelId].list, ...list] : list,
          },
        },
      }

    default:
      return state
  }
}
